from archetypes.schemaextender.interfaces import ISchemaModifier
from Products.Archetypes.atapi import *
from zope.interface import implements, Interface
from zope.component import adapts, provideAdapter

from Products.Poi.interfaces import ITracker

from apkn.poiextender.config import TRACKER_FIELDS_TO_HIDE, TRACKER_FIELDS_TO_MODIFY

class TrackerExtender(object):
    """Schema modifier to hide and rename some fields in the Tracker.
    """
    
    adapts(ITracker)
    implements(ISchemaModifier)
        
    def fiddle(self, schema):
		#Hide certain fields
		for field in TRACKER_FIELDS_TO_HIDE:
			new_field = schema[field].copy()
			new_field.widget.visible = {'edit': 'invisible', 'view': 'invisible'}
			schema[field] = new_field
		
		#Rename fields
		
		for key in TRACKER_FIELDS_TO_MODIFY.keys():	
			new_field = schema[key].copy()
			if TRACKER_FIELDS_TO_MODIFY[key].has_key('label'):
				new_field.widget.label = TRACKER_FIELDS_TO_MODIFY[key]['label']
			if TRACKER_FIELDS_TO_MODIFY[key].has_key('description'):
				new_field.widget.description = TRACKER_FIELDS_TO_MODIFY[key]['description']				
			if TRACKER_FIELDS_TO_MODIFY[key].has_key('label_msgid'):
				new_field.widget.label_msgid = TRACKER_FIELDS_TO_MODIFY[key]['label_msgid']			
			if TRACKER_FIELDS_TO_MODIFY[key].has_key('description_msgid'):
				new_field.widget.description_msgid = TRACKER_FIELDS_TO_MODIFY[key]['description_msgid']			
			if TRACKER_FIELDS_TO_MODIFY[key].has_key('i18n_domain'):
				new_field.widget.i18n_domain = TRACKER_FIELDS_TO_MODIFY[key]['i18n_domain']			
			if TRACKER_FIELDS_TO_MODIFY[key].has_key('default'):	
				new_field.default = TRACKER_FIELDS_TO_MODIFY[key]['default']
			schema[key] = new_field
		
    def __init__(self, context):
        self.context = context	
